package com.example;

//代码生成器

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;


public class DaiMaShengChengQi {

	public static void main(String[] args) {
		//需要构建代码生成器 对象
		AutoGenerator autoGenerator = new AutoGenerator();
		//配置策略

		//1.全局配置
		GlobalConfig gc = new GlobalConfig();
		String projectPath = System.getProperty("user.dir");//获取用户目录
		gc.setOutputDir(projectPath+"/src/main/java");
		gc.setAuthor("作者名字");
		gc.setOpen(false);
		gc.setFileOverride(false);//是否覆盖
		gc.setServiceName("%sService");//去Service的I前缀
		gc.setIdType(IdType.ID_WORKER);
		gc.setDateType(DateType.ONLY_DATE);
		gc.setSwagger2(true);
		autoGenerator.setGlobalConfig(gc);

		//2.设置数据源
		DataSourceConfig dsc = new DataSourceConfig();
		dsc.setUrl("jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8&characterEncoding=utf-8");
		dsc.setDriverName("com.mysql.cj.jdbc.Driver");
		dsc.setUsername("root");
		dsc.setPassword("root");
		dsc.setDbType(DbType.MYSQL);
		autoGenerator.setDataSource(dsc);

		//3.包的配置
		PackageConfig pc = new PackageConfig();
		pc.setModuleName("ceshi"); //生成模块名
		pc.setParent("com.cy"); //生成路径
		pc.setEntity("pojo");
		pc.setMapper("mapper");
		pc.setService("service");
		pc.setController("controller");
		autoGenerator.setPackageInfo(pc);

		//4.策略配置
		StrategyConfig strategy = new StrategyConfig();
		strategy.setInclude("user"); //设置要映射的表名**************
		strategy.setNaming(NamingStrategy.underline_to_camel);
		strategy.setColumnNaming(NamingStrategy.underline_to_camel);
		strategy.setEntityLombokModel(true);//生成lombok

		strategy.setLogicDeleteFieldName("deleted");//逻辑删除 参数为逻辑删除字段
		//自动填充配置
		TableFill createTime = new TableFill("create_time", FieldFill.INSERT);
		TableFill updateTime = new TableFill("update_time", FieldFill.INSERT_UPDATE);
		ArrayList<TableFill> tableFills = new ArrayList<>();
		tableFills.add(createTime);
		tableFills.add(updateTime);
		strategy.setTableFillList(tableFills);
		//乐观锁
		strategy.setVersionFieldName("version");
		//controller使用RestFor风格
		strategy.setRestControllerStyle(true);
		strategy.setControllerMappingHyphenStyle(true);//localhost:8080/hello_id_2

		autoGenerator.setStrategy(strategy);

		autoGenerator.execute(); //执行代码生成器
	}

}
